package com.linzm.leetcode.primary.exercises1_20230114;

import java.util.Arrays;
import java.util.Comparator;
import java.util.TreeMap;

/**
 * @Author zimingl
 * @Date 2023/1/15 15:58
 * @Description: 相对名次
 *
 * 做法： copy arr、 map
 */
public class Demo19_506 {
    /**
     * 输入：score = [10,3,8,9,4]
     * 输出：["Gold Medal","5","Bronze Medal","Silver Medal","4"]
     * 解释：名次为 [1st, 5th, 3rd, 2nd, 4th] 。
     * 名次第 1 的运动员获金牌 "Gold Medal" 。
     * 名次第 2 的运动员获银牌 "Silver Medal" 。
     * 名次第 3 的运动员获铜牌 "Bronze Medal" 。
     */
    public static void main(String[] args) {
        Demo19_506 demo19_506 = new Demo19_506();
        int[] score = new int[]{10, 3, 8, 9, 4};
        String[] resStrings = demo19_506.findRelativeRanks(score);
        System.out.println(String.join(" ", resStrings));
    }

    private String[] findRelativeRanks(int[] score) {
        String[] res = new String[score.length];
        int[] copyScores = Arrays.copyOf(score, score.length);
        int[] scoreSorted = Arrays.stream(copyScores).boxed().sorted(Comparator.reverseOrder()).mapToInt(x -> x).toArray();
        TreeMap<Integer, String> map = new TreeMap<>();
        map.put(1, "Gold Medal");
        map.put(2, "Silver Medal");
        map.put(3, "Bronze Medal");
        for (int i = 0; i < score.length; i++) {
            for (int j = 0; j < score.length; j++) {
                if (scoreSorted[i] == score[j] && i < 3) {
                    res[j] = map.get(i + 1);
                    break;
                }
                if (scoreSorted[i] == score[j] && i >= 3) {
                    res[j] = "" + (i + 1);
                    break;
                }
            }
        }
        return res;
    }
}
